//
//  JWStuClockCtryVC.m
//  test
//
//  Created by 朱圳 on 2018/3/21.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "JWStuClockCtryVC.h"
#import "PYSearch.h"
#import "HasClockcell.h"
#import "SDUserModel.h"
#import "StuClockHisVC.h"
#import "JWSearchStuClockCtryVC.h"
@interface JWStuClockCtryVC ()<UITableViewDelegate,UITableViewDataSource,PYSearchViewControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;//今日未打卡
@property (nonatomic, strong) NSMutableArray *contentArry;

@end

@implementation JWStuClockCtryVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    [self setTitleView];
    [self setdata];
    [self.view addSubview:self.tableView];
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self setdata];
    }];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)setTitleView{
    self.title = @"学员列表";
    UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
    [rightButton setImage:[UIImage imageNamed:@"searchbar_textfield_search_icon"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem= rightItem;
}

- (void)search
{
 //   NSLog(@"search----");
    NSArray *Hotarr = @[@"田涛",@"朱圳",@"李彦宏",@"肖欢芯",@"游鹤亭",@"严戈闲",@"陈炽",@"赵延"];
    PYSearchViewController *searchController = [PYSearchViewController searchViewControllerWithHotSearches:Hotarr searchBarPlaceholder:@"学生姓名" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        JWSearchStuClockCtryVC *vc = [[JWSearchStuClockCtryVC alloc]init];
        vc.searchText = searchText;
        [searchViewController.navigationController pushViewController:vc animated:YES];
    }];
    // 2. 设置风格
    searchController.hotSearchStyle = PYHotSearchStyleBorderTag;
    searchController.hotSearchTitle = @"推荐搜索";
    
    searchController.searchSuggestionHidden=YES;
    // 3. 设置代理
    searchController.delegate = self;
    // 4. 跳转到搜索控制器
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchController];
    [self presentViewController:nav  animated:NO completion:nil];
    
}

/**
 请求数据
 */
-(void)setdata{
    NSDictionary *dict=@{
                         @"jwid" : JWid,
                         @"jwpassword" : JWpassword,
                         };
    
    [[AFNetworkTool shareManager]getJSONWithUrl:JWteacher_Studentlist parame:dict successData:^(id json) {
        [self.tableView.mj_header endRefreshing];
        [self.contentArry removeAllObjects];
        NSArray * arr = json[@"data"];
        __weak typeof(self) weakslf = self;
        for(int i=0;i<arr.count;i++){
            SDUserModel *model = [SDUserModel yy_modelWithJSON:arr[i]];
            [weakslf.contentArry addObject:model ];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArry.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"cell";
    HasClockcell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //根据
    if (cell == nil) {
        
        cell = [[HasClockcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    cell.model = self.contentArry[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 50;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StuClockHisVC *vc = [[StuClockHisVC alloc]init];
    SDUserModel *model = self.contentArry[indexPath.row];
    vc.model = model;
    
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970];
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    if ([model.revisetime isEqualToString:@"0"]) {
        vc.days = @"0";
    }else{
        double days = ([timeString doubleValue] - [model.revisetime doubleValue])/86400 +1;
        vc.days = [NSString stringWithFormat:@"%.f",days];
    }
    
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma -mark 尾部的内容
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    UILabel *la = [[UILabel alloc ]init ];
    la.frame = CGRectMake(0, 0, ScreenWidth , 40);
    la.text = @"--亲,到底了!--";
    la.font = [UIFont systemFontOfSize:15.f];
    la.textAlignment = NSTextAlignmentCenter;
    la.textColor = [UIColor lightGrayColor];
    return la;
}

-(NSMutableArray *)contentArry{
    if(_contentArry==nil){
        _contentArry=[[NSMutableArray alloc]init];
    }
    return _contentArry;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
@end

